package com.sora.service.impl;

import com.sora.enums.ResultCode;
import com.sora.exception.ApiException;
import com.sora.mapper.UserMapper;
import com.sora.model.entity.UserEntity;
import com.sora.model.param.LoginParam;
import com.sora.model.param.RegisterParam;
import com.sora.model.vo.ResultVO;
import com.sora.model.vo.UserVO;
import com.sora.security.JwtManager;
//import com.sora.security.TokenManager;
import com.sora.security.UserDetail;
import com.sora.service.UserService;
import io.swagger.annotations.Api;
import org.dozer.DozerBeanMapper;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;

@Service("userService")
public class UserServiceImpl implements UserService, UserDetailsService {

    @Resource
    PasswordEncoder passwordEncoder;

    @Resource
    JwtManager jwtManager;

//    @Resource
//    TokenManager tokenManager;

    @Resource
    UserMapper userMapper;

    @Resource
    DozerBeanMapper dozerBeanMapper;

    @Override
    public UserVO login(LoginParam param) {
        UserEntity userEntity = userMapper.queryUser(param.getName());
        if (userEntity == null || !passwordEncoder.matches(param.getPassword(), userEntity.getPassword())) {
            System.out.println(userEntity.toString());
            System.out.println(param.getPassword());
            System.out.println(userEntity.getPassword());
            throw new ApiException("账号密码错误");
        }
        UserVO userVO = dozerBeanMapper.map(userEntity, UserVO.class);
//        userVO.setToken(jwtManager.generateShort(param.getName()));
        userVO.setToken(jwtManager.generate(userEntity.getName()));
        return userVO;
//        if (tokenManager.init(userVO.getName())) {
//            return userVO;
//        } else {
//            throw new ApiException("注册失败");
//        }
    }

    @Override
    public String test() {
        return "success";
    }

    @Override
    public UserDetail loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity userEntity = userMapper.queryUser(username);
        if (userEntity == null) {
            throw new UsernameNotFoundException("没有找到该用户");
        }
        return new UserDetail(userEntity, Collections.EMPTY_LIST);
    }

    @Override
    public ResultVO register(RegisterParam param) {
        if (userMapper.queryUser(param.getName()) != null) {
            return new ResultVO(ResultCode.NAME_EXIST);
        }
        UserEntity userEntity = dozerBeanMapper.map(param, UserEntity.class);
        userEntity.setPassword(passwordEncoder.encode(param.getPassword()));
        if (userMapper.addUser(userEntity) > 0) {
            return new ResultVO();
        }
        return new ResultVO(ResultCode.FAILED);
    }

//    @Override
//    public String logout(String username) {
//        tokenManager.destory(username);
//        return "用户注销成功";
//    }

    @Override
    public UserVO updateUser(RegisterParam registerParam) {
        UserEntity userEntity = dozerBeanMapper.map(registerParam, UserEntity.class);
        userEntity.setId(userMapper.queryUser(userEntity.getName()).getId());
        userEntity.setPassword(passwordEncoder.encode(userEntity.getPassword()));
        if (userMapper.updateUser(userEntity) > 0) {
            UserVO userVO = dozerBeanMapper.map(userEntity, UserVO.class);
            System.out.println(userVO.toString());
            return userVO;
        } else {
            throw new ApiException("更新失败");
        }
    }
}
